package basicelement;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import istar.agent.IstarJadeAgent;
import istar.behaviour.IstarBehaviour;
import istar.impl.AbstractBasicResource;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.OneShotBehaviour;

public class Money extends AbstractBasicResource {

	private boolean log = true;
	
	public Money() {
		super("Money"); 
	}

	@Override
	public Behaviour getBehaviour() {
		//return new myBehaviour((IstarJadeAgent)this.getOwner(), this);
		return new myBehaviour(this);
	}
	
	private class myBehaviour extends IstarBehaviour{

		AbstractBasicResource resource;
		
		public myBehaviour(AbstractBasicResource _resource){
			super(_resource);
		}

		@Override
		public void myAction() {
			String moneyFile =null;
			try {
				BufferedReader in = new BufferedReader(new FileReader(((IstarJadeAgent)myAgent).configurationPath));
			    String str;
			    while ((str = in.readLine()) != null) {
			        if (str.startsWith("MoneyFile=")){
			        	moneyFile = str.substring(10);
			        }
			    }
			    in.close();

			    if (moneyFile!=null){
			    	//File f = (new File(moneyFile));
			    	
					BufferedReader m = new BufferedReader(new FileReader(moneyFile));
					String line = m.readLine();
					int index = line.indexOf("Money=");
					
					Double value = new Double(line.substring(index+6));//, line.lastIndexOf(";")-1));
				    this.resource.getContent().clear();
				    this.resource.getContent().add(value);
				    
				    m.close();
			    	
			    }

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		

		@Override
		public int myOnEnd() {
			// TODO Auto-generated method stub
			return 1;
		}

		@Override
		public boolean done() {
			return true;
		}
	}
}

